package com.xxx.video.main.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.xxx.video.commen.configs.Constant;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.SPUtil;
import com.xxx.video.main.beans.HomeTypeListBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

public class HomeFragmentPresenter extends BasePresenter<IHomeView> {

    public HomeFragmentPresenter(@NonNull Context context, IHomeView baseView) {
        super(context, baseView);
    }

    /**
     * get type list
     */
    public void getTypeList() {
        NetWorkUtils.getInstance().getTypeInfoList(new CommonResponseListener<HomeTypeListBean>() {
            @Override
            public void onSuccess(HomeTypeListBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        if (!ListUtils.isEmpty(successOb.typeList)) {
                            SPUtil.putString(Constant.HomeType, new Gson().toJson(successOb));
                        }
                        getBaseView().initTabLayout(successOb.typeList);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                if (getBaseView() != null) {
                    String result = SPUtil.getString(Constant.HomeType, "");
                    if (!TextUtils.isEmpty(result)) {
                        try {
                            HomeTypeListBean successOb = new Gson().fromJson(result, HomeTypeListBean.class);
                            getBaseView().initTabLayout(successOb.typeList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        getBaseView().showRootLayoutStatus(3);
                    }
                }

            }
        });
    }

}
